package com.zahb.qadx.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.model.MainFunc1;
import com.zahb.qadx.model.MainFunc2;
import com.zahb.qadx.ui.activity.NewsListActivity;
import com.zahb.qadx.ui.activity.WebViewActivity;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.DisplayUtil;
import com.zahb.qadx.util.ImageLoaderKt;

import org.jetbrains.annotations.NotNull;

/**
 * Created by Zdw on 2020/10/14 16:52
 */
public class NewsFragment extends StarTeacherFragment {

    static final String TAG = "NewsFragment";

    static NewsFragment newInstance(MainFunc1 mainFunc1) {
        Bundle args = new Bundle();
        args.putSerializable("data", mainFunc1);
        NewsFragment fragment = new NewsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NotNull @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        MainFunc1 mainFunc1 = (MainFunc1) mArguments.getSerializable("data");
        if (mainFunc1 != null) {
            RelativeLayout layout = mRootView.findViewById(R.id.layout_container);
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
            if (CompatHelper.isEmpty(mainFunc1.getBackground())) {
                lp.topMargin = DisplayUtil.dip2px(10f);
                layout.setLayoutParams(lp);
            } else {
                lp.topMargin = DisplayUtil.dip2px(50f);
                layout.setLayoutParams(lp);
                ImageView imageView = mRootView.findViewById(R.id.iv_background);
                ImageLoaderKt.loadImageFit(imageView, mainFunc1.getBackground());
            }
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.ic_home_news, 0, 0, 0);
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<MainFunc2, BaseViewHolder> getAdapter() {
        mAdapter = new NewsAdapter(R.layout.item_list_home_news);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(getActivity(), WebViewActivity.class);
            intent.putExtra("type", "1");
            intent.putExtra("id", mAdapter.getItem(position).getSourceId());
            startActivity(intent);
        });
        return mAdapter;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home_news;
    }

    @Override
    protected void seeMore() {
        MainFunc1 mainFunc1 = (MainFunc1) mArguments.getSerializable("data");
        if (mainFunc1 != null) {
            NewsListActivity.actionStart(getContext(), mainFunc1.getSourceId());
        }
    }

    public static class NewsAdapter extends BaseQuickAdapter<MainFunc2, BaseViewHolder> {

        NewsAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, MainFunc2 mainFunc2) {
            ImageLoaderKt.loadImageSameCorners(baseViewHolder.getView(R.id.iv_icon), mainFunc2.getIcon(), 5f);
            baseViewHolder.setText(R.id.tv_name, mainFunc2.getTitle());
            if (CompatHelper.isEmpty(mainFunc2.getSubTitle())){
                baseViewHolder .setText(R.id.tv_desc, mainFunc2.getTitle());
            }else {
                baseViewHolder .setText(R.id.tv_desc, mainFunc2.getSubTitle());

            }


        }
    }
}
